/*
 * A simple ToDo Manager
 *
 * Copyright (c) 2011 Nosheen Zaza, Manuel Rebollo, Seyedsaeid Mahpeikar and
 * Wo Ke
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package todomanager.ui.tableview;

import java.awt.Component;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import todomanager.model.Task;

/**
 * The renderer of a table that is used to show tasks. It renders {@code Task}
 * objects.
 * 
 * @author
 * Nosheen Zaza
 */
public class TaskCellRenderer implements TableCellRenderer {

    TaskDisplayCellComponent taskDisplay;
    JPanel categoryPanel;
    JPanel priorityPanel;
    JLabel category;
    JLabel priority;

    /**
     * Creates and initializes an instance.
     */
    public TaskCellRenderer() {
        taskDisplay = new TaskDisplayCellComponent();
        
        category = new JLabel();
        category.setOpaque(false);
        
        categoryPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        categoryPanel.add(category);

        priority = new JLabel();
        priority.setOpaque(false);

        priorityPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        priorityPanel.add(priority);

    }

    /**
     * Returns a renderer to a table cell.
     * @param table
     * @param value
     * @param isSelected
     * @param hasFocus
     * @param row
     * @param column
     * @return a suitable renderer based on the column number.
     */
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        Task t = (Task) value;
        if (column == 0) {
            taskDisplay.updateData(t, isSelected);
            return taskDisplay;
        } else if (column == 1) {
            category.setText(t.getCategory());
            return categoryPanel;

        } else {
            priority.setText(t.getPriority());
            return priorityPanel;
        }
    }
}
